﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using DotSpatial.Topology;



namespace HAMath
{

    /// <summary>
    /// 判断一个点和多边形是否有交集
    /// </summary>
    public class PointInPolygonRegion
    {


        private static Coordinate[] cellCoordinates;

        private static Polygon polygon;


        /// <summary>
        /// 解释为什么参数设计成这种格式：外围调用时不认识GIS的Polygon等类型；需要具有通用性
        /// 判断一个点是否和多边形有交集 第一个参数为点的坐标（XYZ顺次添加），第二个参数为多变形的顶点的集合（double []中顺序存放每个顶点的XYZ）
        /// </summary>
        /// <param name="pointCoordinate"> 数组中按照XYZ的顺序存放点的坐标信息</param>
        /// <param name="polygonPointCoordinate">构成多边形的顶点的集合 数组中按照XYZ的顺序存放点的坐标信息</param>
        /// <returns></returns>
        public static bool JudgePointInPolygon(double [] pointCoordinate, List<double[]> polygonPointCoordinate)
        {


            try
            {


                //if (cellCoordinates !=null )
                //{
                //    cellCoordinates .
                //}

                if (pointCoordinate == null || polygonPointCoordinate == null)
                {
                    return false;
                }
                if (pointCoordinate.GetLength (0) != 3 || polygonPointCoordinate.Count < 1)
                {
                    return false;
                }

                for (int i = 0; i < polygonPointCoordinate.Count; i++)
                {
                    if (polygonPointCoordinate[i].GetLength(0) != 3)
                    {
                        return false;
                    }
                }


                //定义点  转换成GIS能够识别的点类型
                DotSpatial.Topology.Point point0 = new DotSpatial.Topology.Point();
                point0.X = pointCoordinate[0];
                point0.Y = pointCoordinate[1];
                point0.Z = pointCoordinate[2];

                cellCoordinates = new Coordinate[polygonPointCoordinate.Count];

                for (int i = 0; i < polygonPointCoordinate.Count; i++)
                {

                    Coordinate coor = new Coordinate();

                    coor.X = polygonPointCoordinate[i][0];
                    coor.Y = polygonPointCoordinate[i][1];
                    coor.Z = polygonPointCoordinate[i][2];


                    cellCoordinates[i] = coor;

                }



                polygon = new Polygon(new LinearRing(cellCoordinates));


                return polygon.Contains(point0);

            }
            catch (Exception)
            {

                return false;
            }



        }



        /// <summary>
        /// 用于判断一个点是否和多边形有交集
        /// </summary>
        /// <param name="point"></param>
        /// <param name="polygon"></param>
        /// <returns></returns>
        //public static bool JudgePointInPolygon(DotSpatial.Topology.Point point, Polygon polygon)
        //{
        //    try
        //    {

        //        if (polygon == null)
        //        {
        //            return false;
        //        }

        //        return polygon.Contains(point);
        //    }
        //    catch (Exception)
        //    {

        //        return false;
        //    }


        //}




        /// <summary>
        /// 测试代码
        /// </summary>

        public static void Judge()
        {

            #region 判断点和多边形是否相交

            //DotSpatial.Topology.Point point0 = new DotSpatial.Topology.Point();
            //point0.X = 0.5;
            //point0.Y = 0.5;
            //point0.Z = 0;


            //Coordinate[] cellCoordinates = new Coordinate[4];

            //Coordinate coor1 = new Coordinate();
            //coor1.X = 0;
            //coor1.Y = 0;
            //coor1.Z = 0;

            //Coordinate coor2 = new Coordinate();
            //coor2.X = 1;
            //coor2.Y = 0;
            //coor2.Z = 0;


            //Coordinate coor3 = new Coordinate();
            //coor3.X = 1;
            //coor3.Y = 1;
            //coor3.Z = 0;


            //Coordinate coor4 = new Coordinate();
            //coor4.X = 0;
            //coor4.Y = 1;
            //coor4.Z = 0;

            //cellCoordinates[0] = coor1;
            //cellCoordinates[1] = coor2;
            //cellCoordinates[2] = coor3;
            //cellCoordinates[3] = coor4;


            ////cellCoordinates[0].X = 0;
            ////cellCoordinates[0].Y = 0;
            ////cellCoordinates[0].Z = 0;

            ////cellCoordinates[1].X = 1;
            ////cellCoordinates[1].Y = 0;
            ////cellCoordinates[1].Z = 0;

            ////cellCoordinates[2].X = 1;
            ////cellCoordinates[2].Y = 1;
            ////cellCoordinates[2].Z = 0;

            ////cellCoordinates[3].X = 0;
            ////cellCoordinates[3].Y = 1;
            ////cellCoordinates[3].Z = 0;


            //Polygon polygon = new Polygon(new LinearRing(cellCoordinates));

            //bool inPolygon = false;
            //inPolygon = polygon.Contains(point0);



            #endregion

            #region 判断线和矩形单元格是否相交

            //Coordinate[] lineCoordinates = new Coordinate[3];

            //Coordinate linecoor1 = new Coordinate();
            //linecoor1.X = -1;
            //linecoor1.Y = -1;
            //linecoor1.Z = 0;

            //Coordinate linecoor2 = new Coordinate();
            //linecoor2.X = 0.5;
            //linecoor2.Y = 0.5;
            //linecoor2.Z = 0;


            //Coordinate linecoor3 = new Coordinate();
            //linecoor3.X = 0.5;
            //linecoor3.Y = 2;
            //linecoor3.Z = 0;




            //cellCoordinates[0] = linecoor1;
            //cellCoordinates[1] = linecoor2;
            //cellCoordinates[2] = linecoor3;

            //List<Coordinate> coordinateList = new List<Coordinate>();

            //coordinateList.Add(linecoor1);
            //coordinateList.Add(linecoor2);
            //coordinateList.Add(linecoor3);






            //LineString linearString = new LineString(coordinateList);//定义了一条线 参数为顺序的几个点


            //polygon.Intersection(linearString);




            #endregion

            #region 判断面和矩形单元格是否相交



            //List<Coordinate> coordinateList = new List<Coordinate>();

            //coordinateList.Add(linecoor1);
            //coordinateList.Add(linecoor2);
            //coordinateList.Add(linecoor3);


            //Polygon polygon2 = new Polygon(new LinearRing(cellCoordinates));


            //polygon.Intersection(polygon2);



            #endregion

            #region 判断多边形是否和矩形单元格相交


            //和上述情况类似




            #endregion



        }


        public static void JudgePointInPolygonRegion(Polygon plg, LineString ls, ref Polygon[] plgs)
        {
            //plgs = new Polygon[] { };


            //DotSpatial.Topology.Point point0 = new DotSpatial.Topology.Point();
            //point0.X = 0.5;
            //point0.Y = 0.5;
            //point0.Z = 0;


            //Coordinate[] cellCoordinates = new Coordinate[4];

            //cellCoordinates[0].X = 0;
            //cellCoordinates[0].Y = 0;
            //cellCoordinates[0].Z = 0;

            //cellCoordinates[1].X = 1;
            //cellCoordinates[1].Y = 0;
            //cellCoordinates[1].Z = 0;

            //cellCoordinates[2].X = 1;
            //cellCoordinates[2].Y = 1;
            //cellCoordinates[2].Z = 0;

            //cellCoordinates[3].X = 0;
            //cellCoordinates[3].Y = 1;
            //cellCoordinates[3].Z = 0;


            //Polygon polygon = new Polygon(new LinearRing(cellCoordinates));

            //polygon.Contains(point0);



            //ILinearRing lr = (LinearRing)plg.Shell;


            //HA.ModFlow.Domain.DomainLayer.GridData gd = ProjectSerivce.DefaultService.Project.Grid3dData.MainGrid.ModFlowGrid;
            //DotSpatial.Topology.Coordinate[, ,] coord3D = AlgorithmHelper.ConvertCoordinatesFor2D(ProjectSerivce.DefaultService.Project.Grid3dData.MainGrid.ModFlowGrid.GetBoundGrid().GridPoints);
            //HyGrid hg = new HyGrid(gd.RowsCount, gd.ColsCount, gd.LayersCount, gd.RowCoords, gd.ColCoords, gd.LayerCoords, gd.OrgX, gd.OrgY, gd.OrgZ, gd.RotationZ, coord3D);

            //Point3D[,,] point3D
            //      DotSpatial.Topology.Coordinate[, ,] coords = new DotSpatial.Topology.Coordinate[point3D.GetLength(2), point3D.GetLength(0), point3D.GetLength(1)];
            //Point3D pt;
            //for (int k = 0; k < point3D.GetLength(2); k++)
            //{
            //    for (int i = 0; i < point3D.GetLength(0); i++)
            //    {
            //        for (int j = 0; j < point3D.GetLength(1); j++)
            //        {

            //            pt = point3D[i, j, k];

            //            coords[k, i, j] = new DotSpatial.Topology.Coordinate(pt.X, pt.Y, pt.Z);

            //        }

            //    }

            //}


        }







    }
}
